#include <stdio.h>
#include <sys/types.h>          
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <signal.h>
#include <sys/select.h>

struct Data{
	int code;
	char name[32];
	char data[500];
};

int main(int argc, char const *argv[])
{
	int sockfd;
	if(-1==(sockfd=socket(AF_INET,SOCK_DGRAM,0)))
	{
		perror("socket");
		return -1;
	}

	struct Data toserpack;
	toserpack.code=0;
	memset(toserpack.data,0,500);
	memset(toserpack.name,0,32);
	char username[32]={0}; 
	char message[500]={0};
	char recvmess[500]={0};
	char recvname[32]={0};
	struct sockaddr_in seraddr;

	socklen_t addrlen=sizeof(seraddr);

	seraddr.sin_family=AF_INET;
	seraddr.sin_addr.s_addr=inet_addr(argv[1]);
	seraddr.sin_port=htons(atoi(argv[2]));

	printf("plz input your name>>\n");
	fgets(username,32,stdin);
	username[strlen(username)-1]='\0';
	toserpack.code=1;
	strcpy((toserpack.name),username);

	if(-1==(sendto(sockfd,&toserpack,sizeof(toserpack),0,(struct sockaddr *)&seraddr,addrlen)))
	{
		perror("send name");
		return -1;
	}


	fd_set readfds; 
	FD_ZERO(&readfds);


	while(1)
	{
		FD_SET(0,&readfds);
		FD_SET(sockfd,&readfds);
		select(sockfd+1,&readfds,NULL,NULL,NULL);
		if(FD_ISSET(0,&readfds)==1)
		{
			fgets(message,500,stdin);
			message[strlen(message)-1]='\0';

			if(strcmp(message,"quit")==0)
			{
				toserpack.code=3;
				strcpy((toserpack.data),message);
				if(-1==(sendto(sockfd,&toserpack,sizeof(toserpack),0,(struct sockaddr *)&seraddr,addrlen)))
				{
					perror("send message");
					return -1;
				}
				memset(toserpack.data,0,500);
//				kill(0,SIGKILL);
				break;
			}
			else
			{
				toserpack.code=2;
				strcpy((toserpack.data),message);
				if(-1==(sendto(sockfd,&toserpack,sizeof(toserpack),0,(struct sockaddr *)&seraddr,addrlen)))
				{
					perror("send message");
					return -1;
				}
				memset(toserpack.data,0,500);
			}
		}

		if(FD_ISSET(sockfd,&readfds)==1)
		{
			recvfrom(sockfd,recvmess,sizeof(recvmess),0,(struct sockaddr *)&seraddr,&addrlen);
			//            printf("recv over\n");
			strncpy(recvname,recvmess,strlen(username));
			printf("%s\n",recvmess);
		}
	}



	/*
	   pid_t pid=fork();
	   if(pid>0)
	   {
	   while(1)
	   {
	//            printf("plz input message\n");
	fgets(message,500,stdin);
	message[strlen(message)-1]='\0';

	if(strcmp(message,"quit")==0)
	{
	toserpack.code=3;
	strcpy((toserpack.data),message);
	if(-1==(sendto(sockfd,&toserpack,sizeof(toserpack),0,(struct sockaddr *)&seraddr,addrlen)))
	{
	perror("send message");
	return -1;
	}
	memset(toserpack.data,0,500);
	kill(0,SIGKILL);
	break;
	}
	else
	{
	toserpack.code=2;
	strcpy((toserpack.data),message);
	if(-1==(sendto(sockfd,&toserpack,sizeof(toserpack),0,(struct sockaddr *)&seraddr,addrlen)))
	{
	perror("send message");
	return -1;
	}
	memset(toserpack.data,0,500);
	}
	}
	}
	else if(pid==0)
	{
	while(1)
	{
	//            printf("waitrecv\n");
	recvfrom(sockfd,recvmess,sizeof(recvmess),0,(struct sockaddr *)&seraddr,&addrlen);
	//            printf("recv over\n");
	strncpy(recvname,recvmess,strlen(username));
	printf("%s\n",recvmess);

	if(strcmp(recvname,username)!=0)
	{
	printf("%s\n",recvmess);
	}

	}
	}
	*/
	return 0;
}
